libcode1.a:code1.o
	@ar -rc $@ $^                          #archive/replace/create
	@echo "build $^ to $@ ... done"

libcode2.so:code2.o
	@g++ -shared -fPIC -o $@ $^            #GUN c++ compiler/Position-Independent Code
	@echo "build $^ to $@ ... done"

code1.o:code1.cpp
	@g++ -c -g -o $@ $^                     #compile/debug information
	@echo "compiling $^ to $@ ... done"

code2.o:code2.cpp
	@g++ -fPIC -c -g  -o $@ $^
	@echo "compiling $^ to $@ ... done"


test.o:test.cpp
	@g++ -c -g -o $@ $^
	@echo "compiling $^ to $@ ... done"

test.exe: test.o libcode1.a libcode2.so
	@g++ -o $@ $< -L. -lcode1 -lcode2      #这里-L. 作用于-lcode1 -lcode2默认到系统标准库路径寻找
	@echo "link $^ to produce $@ ...done"

.PHONY: all
all: libcode1.a libcode2.so test.exe

.PHONY: run                                #PHONY伪目标,make run时创建子进程
run: test.exe
	@echo "Running $<..."
	@LD_LIBRARY_PATH=./:$LD_LIBRARY_PATH ./$^
.PHONY:clean
clean:
	@rm -rf *o *.a *.so stdc* *.exe
	@echo "clean ... done"

.PHONY:output
output:
	@mkdir -p stdc/include
	@mkdir -p stdc/lib
	@cp -f *.h stdc/include
	@cp -f *.a  *.so stdc/lib
	@tar -czf stdc.tgz stdc
	@echo "output stdc ... done"